cmake_minimum_required(VERSION 2.6)
if(COMMAND CMAKE_POLICY)
  cmake_policy(SET CMP0003 NEW)
  cmake_policy(SET CMP0048 NEW)
endif(COMMAND CMAKE_POLICY)

project(cg_examples VERSION 1.1.0)

set(CMAKE_MODULE_PATH "${cg_examples_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_INSTALL_PREFIX "${cg_examples_BINARY_DIR}/dist" CACHE PATH "cg_examples install prefix" FORCE)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenGL REQUIRED)

if(APPLE)
find_package(GLUT REQUIRED)
else()
find_package(FreeGLUT REQUIRED)
set(GLUT_LIBRARIES FreeGLUT::freeglut)
endif()

find_package(GLEW QUIET)
find_package(FreeImage QUIET)
find_package(assimp CONFIG QUIET)

if(assimp_CONFIG)
set(ASSIMP_FOUND TRUE)
endif()

if(WIN32)
include(CopyDependencies)
install_dependency_from_target(FreeGLUT::freeglut)
if(GLEW_FOUND)
install_dependency_from_target(GLEW::glew)
endif()
if(FREEIMAGE_FOUND)
install_external_dll(${FREEIMAGE_DIR} ${FREEIMAGE_DIR} FreeImaged FreeImage)
endif()
if(ASSIMP_FOUND)
#get_target_property(ASSIMP_IMPLIB assimp::assimp IMPORTED_IMPLIB_RELEASE)
#message(STATUS "ASSIMP_IMPLIB=${ASSIMP_IMPLIB}")
install_dependency_from_target(assimp::assimp)
endif()
endif()

include_directories(
  "include"
)

add_custom_target(copy_data ${CMAKE_COMMAND} -E copy_directory ${cg_examples_SOURCE_DIR}/data ${cg_examples_BINARY_DIR}/data)

install(DIRECTORY data/ DESTINATION data PATTERN ".svn" EXCLUDE)
install(DIRECTORY src/shaders/ DESTINATION shaders PATTERN ".svn" EXCLUDE)

function(add_example EXAMPLE_NAME)
  set(LIBRARIES ${GLUT_LIBRARIES} ${OPENGL_LIBRARIES})
  if(ARGV1)
    set(ADDITIONAL_LIBRARYS ${ARGV})
	list(REMOVE_AT ADDITIONAL_LIBRARYS 0)
	list(APPEND LIBRARIES ${ADDITIONAL_LIBRARYS})
  endif(ARGV1)
  add_executable(${EXAMPLE_NAME} src/${EXAMPLE_NAME}.cpp)
  target_link_libraries(${EXAMPLE_NAME} ${LIBRARIES})
  install(TARGETS ${EXAMPLE_NAME} RUNTIME DESTINATION . CONFIGURATIONS Release)  
endfunction(add_example)

add_example(sierpinski)
add_example(picksquare)
add_example(transform)
add_example(sphere)
add_example(curves)
add_example(nurbscurve)
add_example(surface)
add_example(gravity)
add_example(gravity2)
add_example(mandelsimp)

if(FREEIMAGE_FOUND)
  include_directories(${FREEIMAGE_INCLUDE_DIR})
  add_example(texture2d ${FREEIMAGE_LIBRARIES})
  add_dependencies(texture2d copy_data)
  add_example(mirror ${FREEIMAGE_LIBRARIES})
  add_dependencies(mirror copy_data)
  add_example(planet ${FREEIMAGE_LIBRARIES})
  add_dependencies(planet copy_data)
  add_example(spheremap ${FREEIMAGE_LIBRARIES})
  add_dependencies(spheremap copy_data)
endif()

if(FREEIMAGE_FOUND AND ASSIMP_FOUND)
  include_directories(${FREEIMAGE_INCLUDE_DIR})
  add_example(dyncubemap assimp::assimp ${FREEIMAGE_LIBRARIES})
  add_dependencies(dyncubemap copy_data)
endif()

configure_file(src/shaders/phong.vert ${cg_examples_BINARY_DIR}/shaders/phong.vert COPYONLY)
configure_file(src/shaders/phong.frag ${cg_examples_BINARY_DIR}/shaders/phong.frag COPYONLY)
if(WIN32 AND GLEW_FOUND)
  add_example(phongshader GLEW::glew)
else()
  add_example(phongshader)
endif()
